<?php
use yii\db\Migration;

class m150912_123001_storage_tables extends Migration
{

    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci';
        }
        
        $this->createTable('{{%key_storage_item}}', [
            'key' => $this->string(128)
                ->notNull(),
            'value' => $this->text()
                ->notNull(),
            'remark' => $this->text(),
            'updated_at' => $this->integer(),
            'created_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%file_storage_item}}', [
            'id' => $this->primaryKey(),
            'component' => $this->string()
                ->notNull(),
            'path' => $this->string(1024)
                ->notNull(),
            'url' => $this->string(1024)
                ->notNull(),
            'type' => $this->string(),
            'size' => $this->integer(),
            'name' => $this->string(),
            'upload_ip' => $this->string(15),
            'created_at' => $this->integer()
                ->notNull()
        ], $tableOptions);
        
        $this->addPrimaryKey('pk_key_storage_item_key', '{{%key_storage_item}}', 'key');
        $this->createIndex('idx_key_storage_item_key', '{{%key_storage_item}}', 'key', true);
    }

    public function safeDown()
    {
        $this->dropTable('{{%file_storage_item}}');
        $this->dropTable('{{%key_storage_item}}');
    }
}
